package com.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtil {

    private static SqlSessionFactory sessionFactory;

    static {
        try {
            // 加载 MyBatis 配置文件
            InputStream is = Resources.getResourceAsStream("mybatis.xml");

            // 创建 SqlSessionFactory 对象
            sessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取 SqlSession 对象
    public static SqlSession openSqlSession() {
        return sessionFactory.openSession();
    }
}
